package com.she.olympic;

import ken.app.bean.GetStoreInfoBean;
import ken.app.helper.Helper;

import com.she.olympic.R;

import com.adon.ar.test.ARExampleActivity;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.webkit.DownloadListener;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.webkit.WebSettings.ZoomDensity;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class StoreInfoPageActivity extends Activity {

	private ImageView back;
	private TextView title;
	private WebView webView;
	private GetStoreInfoBean setStoreInfoBean;

	private WebViewClient mWebViewClient = new WebViewClient() {
		public boolean shouldOverrideUrlLoading(WebView view, final String url) {
			if (url.startsWith("tel")) {
				String[] title = { "取消", "通話" };
				Helper.ShowMessage(StoreInfoPageActivity.this, "", url.replaceAll("tel:", ""), 2, title, null, null,
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog, int which) {
								Intent callIntent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
								startActivity(callIntent);
							}
						});
				return true;
			}
			return false;
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.store_page);

		Bundle bundle = this.getIntent().getExtras();
		ImageView icon = (ImageView) findViewById(R.id.store_icon);
		back = (ImageView) findViewById(R.id.store_back);
		ImageView bg = (ImageView) findViewById(R.id.store_bg);
		title = (TextView) findViewById(R.id.store_title);
		if (bundle.getInt("type") == 1) {// 1288
			icon.setImageResource(R.drawable.icon_store_01l);
		} else if (bundle.getInt("type") == 2) {// 大團購
			icon.setImageResource(R.drawable.icon_store_02l);
		} else {// 異業
			icon.setImageDrawable(null);
			bg.setImageBitmap(Helper.readBitMap(this, R.drawable.store3_top_bg1));
			back.setImageBitmap(Helper.readBitMap(this, R.drawable.store3_top_b));
			title.setTextColor(0xff003399);
		}

		setStoreInfoBean = ((Olympic) getApplicationContext()).getStoreInfoBean();
		back.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				StoreInfoPageActivity.this.finish();
				System.gc();
			}
		});

		if (setStoreInfoBean.getName().length() >= 8) {
			title.setText(setStoreInfoBean.getName().substring(0, 8) + "...");
		} else {
			title.setText(setStoreInfoBean.getName());
		}
		webView = (WebView) findViewById(R.id.store_webview);
		webView.setWebViewClient(mWebViewClient);
		webView.getSettings().setJavaScriptEnabled(true);
		webView.getSettings().setAppCacheEnabled(true);
		webView.loadUrl(setStoreInfoBean.getUrl());
		webView.getSettings().setSupportZoom(true);
		webView.getSettings().setLoadWithOverviewMode(true);
		// webView.getSettings().setDefaultZoom(ZoomDensity.CLOSE);
		webView.getSettings().setUseWideViewPort(true);
		webView.setInitialScale(50);
		webView.getSettings().setUserAgentString("Android");
		webView.requestFocus();
		webView.setDownloadListener(new DownloadListener() {
			@Override
			public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
				// 實現下載的代碼
				Uri uri = Uri.parse(url);
				// Uri uri = Uri.parse("http://10.10.10.173:8080/1/1.apk");
				Intent intent = new Intent(Intent.ACTION_VIEW, uri);
				startActivity(intent);
			}
		});
	}

	@Override
	public void onBackPressed() {
		StoreInfoPageActivity.this.finish();
		System.gc();
		super.onBackPressed();
	}
}
